package com.phuongtn2.karaoke.adapter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.phuongtn2.karaoke.R;
import com.phuongtn2.karaoke.database.DatabaseHelper;
import com.phuongtn2.karaoke.database.dto.RoomDto;

public class SettingsRoomAdapter extends BaseAdapter {
	private Context context;
	private List<RoomDto> dtos = new ArrayList<RoomDto>();
	private DatabaseHelper databaseHelper;

	public SettingsRoomAdapter(Context context, List<RoomDto> dtos,
			Activity activity) {
		this.context = context;
		this.dtos = dtos;
	}

	@Override
	public View getView(final int position, View convertView,
			ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		databaseHelper = new DatabaseHelper(context);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.room_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.txtRoomName = (TextView) convertView
					.findViewById(R.id.txtRoomName);
			viewHolder.txtRoomStatus = (TextView) convertView
					.findViewById(R.id.txtRoomStatus);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.txtRoomName.setText(dtos.get(position).getRoomName());
		viewHolder.txtRoomStatus.setText(dtos.get(position).getRoomStatus());
		return convertView;
	}

	public void updateUI(ViewHolder holder) {
		dtos = databaseHelper.getAllRoom();
		for (Iterator<RoomDto> iterator = dtos.iterator(); iterator.hasNext();) {
			RoomDto type = (RoomDto) iterator.next();
			holder.txtRoomStatus.setText(type.getRoomStatus());
		}
	}

	class ViewHolder {
		TextView txtRoomName;
		TextView txtRoomStatus;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dtos.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

}
